package com.whfc.ms.api.sys.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.dto.SysAlgorithmTypeDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.SysAlgorithmTypeAddParam;
import com.whfc.fuum.param.SysAlgorithmTypeEditParam;
import com.whfc.ms.api.sys.service.MsAlgorithmService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Description: 系统设置中算法管理模块
 * @Author: likang
 * @Date: 2020-09-22 15:51
 */
@RequiresPermissions(value = {"sys/algType"})
@RestController
@RequestMapping("/ms/api/sys/algorithm")
public class SysAlgorithmController {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsAlgorithmService sysAlgorithmService;


    /**
     * 算法类型：添加算法类型
     */
    @PostMapping("add")
    public Result addAlgorithmType(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                   @RequestBody @Validated SysAlgorithmTypeAddParam param) {
        logger.info("算法类型：添加算法类型，sysUser:{},param:{},", sysUser.toString(), param.toString());
        sysAlgorithmService.addAlgorithmType(param);
        return ResultUtil.success();
    }

    /**
     * 算法类型：编辑算法类型
     */
    @PostMapping("edit")
    public Result editAlgorithmType(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                    @RequestBody @Validated SysAlgorithmTypeEditParam param) {
        logger.info("算法类型：编辑算法类型，sysUser:{},param:{},", sysUser.toString(), param.toString());
        sysAlgorithmService.editAlgorithmType(param);
        return ResultUtil.success();
    }

    /**
     * 算法类型：启用/禁用算法类型
     */
    @PostMapping("state/modify/{id}/{state}")
    public Result disableAlgorithmType(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                       @PathVariable("id") Integer id,
                                       @PathVariable("state") Integer state) {
        logger.info("算法类型：启用/禁用算法类型，sysUser:{},id:{},state:{}", sysUser.toString(), id, state);
        sysAlgorithmService.modifyAlgorithmState(id, state);
        return ResultUtil.success();
    }

    /**
     * 算法类型：删除算法类型
     */
    @PostMapping("del/{id}")
    public Result delAlgorithmType(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                   @PathVariable("id") Integer id) {
        logger.info("算法类型：删除算法类型，sysUser:{},id:{},", sysUser.toString(), id);
        sysAlgorithmService.deleteAlgorithmType(id);
        return ResultUtil.success();
    }

    /**
     * 算法类型：获取算法类型列表
     */
    @GetMapping(value = "list", params = {"pageNum", "pageSize"})
    public Result getAlgorithmTypeList(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                       @RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize,
                                       @RequestParam(value = "keyword", required = false) String keyword,
                                       @RequestParam(value = "state", required = false) Integer state) {
        logger.info("获取算法类型列表(分页), sysUser:{}, keyword:{}, state:{}, pageNum:{}, pageSize:{}",
                sysUser.toString(), keyword, state, pageNum, pageSize);
        PageData<SysAlgorithmTypeDTO> list = sysAlgorithmService.getAlgorithmTypeList(keyword, state, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    /**
     * 算法类型：获取算法类型列表
     */
    @GetMapping("list")
    public Result getAlgorithmTypeList(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                       @RequestParam(value = "keyword", required = false) String keyword,
                                       @RequestParam(value = "state", required = false) Integer state) {
        logger.info("获取算法类型列表(不分页)，sysUser:{}, keyword:{}, state:{}", sysUser.toString(), keyword, state);
        List<SysAlgorithmTypeDTO> list = sysAlgorithmService.getAlgorithmTypeList(keyword, state);
        return ResultUtil.success(new ListData<>(list));
    }

    /**
     * 算法类型-上传封面图片
     *
     * @param user
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploadImg")
    public Result uploadImg(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        logger.info("算法类型：-上传封面图片,user:{},file:{}", user.getUsername(), file.toString());
        String path = sysAlgorithmService.uploadImg(file);
        return ResultUtil.success(path);
    }


}
